/* Copyright Statement:
 *
 * This software/firmware and related documentation ("MediaTek Software") are
 * protected under relevant copyright laws. The information contained herein
 * is confidential and proprietary to MediaTek Inc. and/or its licensors.
 * Without the prior written permission of MediaTek inc. and/or its licensors,
 * any reproduction, modification, use or disclosure of MediaTek Software,
 * and information contained herein, in whole or in part, shall be strictly prohibited.
 */
/* MediaTek Inc. (C) 2017. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
 * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
 * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
 * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
 * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
 * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
 * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
 * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
 * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
 * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
 * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
 * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
 * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
 * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
 *
 * The following software/firmware and/or related documentation ("MediaTek Software")
 * have been modified by MediaTek Inc. All revisions are subject to any receiver's
 * applicable license agreements with MediaTek Inc.
 */

package com.mediatek.settings.advancedcalling;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.telephony.SubscriptionManager;
import android.util.Log;
import android.view.View;

import com.android.ims.ImsException;
import com.android.ims.ImsManager;
import com.android.settings.R;

/**
 * Adding Advanced Calling Settings.
 */
public class AdvancedCallingSettings extends PreferenceActivity {

    private static final String TAG = "Op12AdvancedCallingsSettings";
    static final String USERCONSENT = "user_consent";
    private Preference mActivateAdvancedCallingButton;
    private Preference mAdvancedCallingButton;
    private Preference mWfcCalling;
    private IntentFilter mIntentFilter;
    private BroadcastReceiver mWfcReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.d(TAG, "onReceive()... " + action);
            if (action.equals("com.android.intent.action.IMS_CONFIG_CHANGED")) {
                handleProvision();
            }
        }
    };

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        addPreferencesFromResource(R.xml.advanced_calling_settings);
        Log.d(TAG, "onCreated.");
        mActivateAdvancedCallingButton =  findPreference("activate_advanced_calling_key");
        mAdvancedCallingButton =  findPreference("advanced_calling_key");
        mWfcCalling =  findPreference("wifi_calling_key");
        mActivateAdvancedCallingButton.setOnPreferenceClickListener(
                             new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                try {
                    Intent intent = new Intent("com.vzw.hss.intent.action.PROVISION_VOLTE");
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Log.d(TAG, "Will Open Verizon app");
                    startActivity(intent);
                } catch (ActivityNotFoundException e) {
                        Log.e(TAG, e.toString());
                }
                return true;
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
        unregisterReceiver(mWfcReceiver);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResumed");
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction("com.android.intent.action.IMS_CONFIG_CHANGED");
        registerReceiver(mWfcReceiver, mIntentFilter);
        handleProvision();
    }

    private void handleProvision() {
        Log.d(TAG, "handleProvision entry");
        Preference advancedCallingButton =  findPreference("advanced_calling_key");
        Preference wfcCalling =  findPreference("wifi_calling_key");
        if (!ImsManager.isWfcEnabledByPlatform(this)) {
            if (null != wfcCalling) {
                getPreferenceScreen().removePreference(wfcCalling);
            }
        } else {
            if (null == wfcCalling) {
                getPreferenceScreen().addPreference(mWfcCalling);
            }
        }
        if (ImsManager.isVolteProvisionedOnDevice(this)) {
            Log.d(TAG, "Volte is provisioned");
            mActivateAdvancedCallingButton.setEnabled(false);
            mActivateAdvancedCallingButton.setSummary(getString(
                                  R.string.activated_advanced_calling_summary));
            if (advancedCallingButton == null) {
                getPreferenceScreen().addPreference(mAdvancedCallingButton);
            }
        } else {
            Log.d(TAG, "Volte is de-provisioned");
            mActivateAdvancedCallingButton.setEnabled(true);
            mActivateAdvancedCallingButton.setSummary(getString(
                                  R.string.activate_advanced_calling_summary));
            if (advancedCallingButton != null) {
                getPreferenceScreen().removePreference(advancedCallingButton);
            }
        }
    }
}